'''
Created on April 3, 2011

@author: Jon McGrath
'''
import os.path

import elementtree.ElementTree as ET
from elementtree.ElementTree import Element, SubElement, parse, tostring, iselement

class XMLHandler:
    '''
    This class implements XML manipulation with Python and is used primarily by FileHandler
    '''
    
    root = Element("root")
    
    def __init__(self):
        pass
    
    def loadFromFile(self,filename):
        self.filename = filename
        self.tree = parse(filename)
        return self.tree.getroot()
    
    def writeToFile(self,filename):
        self.filename=filename
        self.tree.write(filename)
        
    def setAttribute(self,attribName,attribValue):
        self.tree.getroot().attrib[attribName]=attribValue
        
    def setSubElement(self,seName,seValue):
        root = self.tree.getroot()
        subelement = ET.SubElement(root,seName)
        subelement.text=seValue
        self.tree = ET.ElementTree(root)
        
    def clearSubElements(self):
        self.tree.getroot().clear()
        
    def getElementText(self,elementName):
        return self.tree.findtext(elementName)
    
    def printXML(self):
        print tostring(self.tree.getroot())
    
    def printTags(self):
        self.__printXMLRecursive(self.tree.getroot(), 0)
        
    def defaultXML(self):
        return "<project></project>"
        
    def getFilename(self):
        return self.filename    
    
    def __printXMLRecursive(self,root,count):
        #print ("\t"*count) + root.tag
        for node in root:
                self.__printXMLRecursive(node,count+1)
        